import { freeReference, mainPointGenerate } from '@/services/ant-design-pro/highClassWrite';
import { Dropdown, message } from 'antd';
import { nanoid } from 'nanoid';
import React from 'react';
import { connect } from 'umi';
const DropDownSelect: React.FC<any> = ({
  children,
  item,
  highClassWrite,
  triggerReference,
  triggerArticleMain,
  setAILogs,
}) => {
  const { writing_session_id, curDoc } = highClassWrite;
  const MarkersSelect = [
    {
      label: (
        <div className="findInfoHandleTag">
          <span className="findInfoHandleDot"></span>标记参考观点
        </div>
      ),
      key: '1',
    },
    {
      label: (
        <div className="findInfoHandleTag">
          <span className="findInfoHandleDot blue"></span>标记参考格式
        </div>
      ),
      key: '2',
    },
    {
      label: (
        <div className="findInfoHandleTag">
          <span className="findInfoHandleDot green"></span>标记参考内容
        </div>
      ),
      key: '3',
    },
  ];
  const findInfoHandleClick = async (v: any, item: any) => {
    console.log(v);
    let material_type = v.key === '1' ? '观点资料' : v.key === '2' ? '结构资料' : '内容资料';
    let body: any = {
      writing_session_id: writing_session_id,
      material_type: material_type,
      main_body: item.main_body,
    };
    let newFormData: any = new FormData();
    //将formObj的参数添加到newFormData里
    Object.keys(body).forEach((key) => {
      newFormData.append(key, body[key]);
    });
    let res = await freeReference(newFormData);
    if (res.code === 0) {
      await triggerReference(v.key); //更新参考内容
      let systemTxtRecover = {
        key: nanoid(),
        type: 'systemTxtRecover',
        spend: 30,
        data: [`小麦已将内容添加到${material_type}`],
      };
      setAILogs((prev: any) => [...prev, systemTxtRecover]);
      //标记参考内容 只添加到参考空间 不更新大纲
      if (v.key !== '3') {
        //没有文章的 手动 调用接口 更新大纲
        if (!curDoc) {
          setTimeout(async () => {
            let systemTxtRecover2 = {
              key: nanoid(),
              type: 'systemTxtRecover',
              spend: 30,
              data: [`小麦正在更新大纲…`],
            };
            setAILogs((prev: any) => [...prev, systemTxtRecover2]);
            //手动 调用接口 更新大纲
            let res2 = await mainPointGenerate({ write_session_id: writing_session_id });
            if (res2.code === 0) {
              triggerArticleMain(); //更新文章大纲
            }
          }, 1200);
        } else {
          //有文章的 出现是否更新文章大纲的提示框
          setTimeout(async () => {
            let updateMain = {
              key: nanoid(),
              type: 'updateMain',
              data: null,
            };
            setAILogs((prev: any) => [...prev, updateMain]);
          }, 1200);
        }
      }
    } else {
      message.open({
        type: 'error',
        content: res.msg,
      });
    }
  };

  return (
    <Dropdown
      placement="bottom"
      menu={{ items: MarkersSelect, onClick: (v) => findInfoHandleClick(v, item) }}
      className="findInfoHandleDropDown"
    >
      {children}
    </Dropdown>
  );
};
// export default DropDownSelect;
export default connect(({ highClassWrite }) => ({ highClassWrite }))(DropDownSelect);
